import { MineModel } from '../../../models/mine.js'
const ModelMine = new MineModel()
import { AppModel } from '../../../models/app.js'
const ModelApp = new AppModel()
const App = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfoModel: {
      nickName: '',
      userCode: '',
      userGold: 0,
      userImg: '%ASSETS_IMG%/c-default-face1@2x.png',
      userType: ''
    }, // 1：客户 2：掌柜 3：运营商
    SignState: false,
    gold: '',
    orderNumber: {
      couponAvailable: 0
    },
    statusBarHeight: App.globalData.statusBarHeight,
    iphoneX: App.globalData.isIphoneX,
    showBg: true,
    isTabbar: true,
    bindPhoneVisible: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.updateUserInfo()
    //查看数量
    this.selectNumber()
    // 隐藏右上角分享功能
    wx.hideShareMenu()
  },
  onShow: function() {
    // // 如果没有用户信息
    // ModelMine.getUserInfo()
    //   .then(res => {
    //     this.getUserInfo(res.data.userInfo)
    //     //查看数量
    //     this.selectNumber()
    //   })
    //   .catch()
    // wx.stopPullDownRefresh()
  },
  tapToOrderList(event) {
    wx.navigateTo({
      url: '/pages/account/order/list/index'
    })
  },

  tapToUserInfo(event) {
    wx.navigateTo({
      url: '/pages/account/detail/index'
    })
  },

  tapToServiceList(event) {
    wx.navigateTo({
      url: '/pages/account/service/list/index'
    })
  },

  tapToAddress(event) {
    wx.navigateTo({
      url: '/pages/account/address/index?form=account'
    })
  },
  //查看优惠券
  tapToCoupon(event) {
    wx.navigateTo({
      url: '/pages/account/coupon/index'
    })
  },
  //查看金币明细
  tapToGoldDetail(event) {
    wx.navigateTo({
      url: '/pages/account/gold/detail/index'
    })
  },

  //查看数量
  selectNumber() {
    const self = this
    const params = {}
    ModelApp.selectNumber(params)
      .then(res => {
        self.setData({
          'orderNumber.orderCreated': res.data.number.orderCreated,
          'orderNumber.orderPaid': res.data.number.orderPaid,
          'orderNumber.orderPicked': res.data.number.orderPicked,
          'orderNumber.orderDelivered': res.data.number.orderDelivered,
          'orderNumber.orderRevoked': res.data.number.orderRevoked,
          'orderNumber.orderRefund': res.data.number.orderRefund,
          'orderNumber.couponAvailable': res.data.number.couponAvailable
        })
        wx.stopPullDownRefresh()
      })
      .catch()
  },

  //根据点击查看不同状态的订单列表
  tapToOrderListDeatail(event) {
    const index = event.currentTarget.dataset.index
    wx.navigateTo({
      url: '/pages/account/order/list/index?index=' + index
    })
  },

  //签到
  sign() {
    //改变签到状态
    const params = {}
    ModelMine.Sign(params).then(res => {
      this.setData({
        gold: res.data.gold,
        SignState: !this.data.SignState
      })
    })
  },

  //点击图片外区域隐藏签到成功信息
  clickMask() {
    this.setData({
      SignState: !this.data.SignState
    })
  },

  doShare() {
    // 分享
    this.setData({
      showShare: true
    })
  },

  //每日签到
  signEveryday() {
    wx.navigateTo({
      url: '/pages/marketing/shareSign/index'
    })
  },
  //新人福利
  welfareNew() {
    wx.navigateTo({
      url: '/pages/manager/task/index'
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    const ucode = App.globalData.userInfo.userCode
    //  const ucode = wx.getStorageSync('ucode')
    console.log(`分享我的个人中心+/pages/home/index?bossid=${ucode}`)
    return {
      title: '小蚁买手',
      path: `/pages/home/index?bossid=${ucode}`,
      imageUrl: '',
      success: function(res) {},
      fail: function(res) {
        // 转发失败
        console.log('转发失败:' + JSON.stringify(res))
      }
    }
  },

  // 更新用户信息
  updateUserInfo() {
    const _this = this
    ModelApp._getVipInfo().then(res => {
      _this.setData({
        'userInfoModel.nickName': res.data.userInfo.nickName,
        'userInfoModel.userCode': res.data.userInfo.userCode,
        'userInfoModel.userImg': res.data.userInfo.userImg,
        'userInfoModel.userGold': res.data.userInfo.userGold,
        'userInfoModel.userType': res.data.userInfo.userType
      })
      App.globalData.userInfo = res.data.userInfo
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    //查看数量
    this.selectNumber()
    // 更新用户信息
    this.updateUserInfo()
  },
  // 二维码
  tapToQrcode(e) {
    const type = e.currentTarget.dataset.type
    const _this = this
    if (type == 'user') {
      if (!App.globalData.userInfo.userPhone || App.globalData.userInfo.userPhone == 0) {
        _this.setData({
          bindPhoneVisible: true
        })
        return false
      }
      const phone = App.globalData.userInfo.userPhone
      wx.navigateTo({
        url: '/pages/account/qrcode/index?type=' + type + '&phone=' + phone
      })
    } else {
      wx.navigateTo({
        url: '/pages/account/qrcode/index?type=' + type
      })
    }
  },

  // 我的微信
  tapToWxcode() {
    wx.navigateTo({
      url: '/pages/account/wx/index'
    })
  }
})
